<?php

class Event extends Zend_Db_Table_Row_Abstract
{
    protected $table;

    public function getTable() {
	if(is_null($this->table)) {
	    require_once 'EventTable.php';
	    $this->table = new EventTable();
	}
	return $this->table;
    }

    public function save(array $data) {
	$table = $this->getTable();
	$fields = $table->info(Zend_Db_Table_Abstract::COLS);

	foreach($data as $field => $value) {
	    if(!in_array($field, $fields)) {
		unset($data[$field]);
	    }
	}

	return $table->insert($data);
    }

    public function getAllEvents() {
	$table = $this->getTable();
	$select = $table->select()->order('ts_creation');

	return $table->fetchAll($select);
    }

    public function getLatestEvent($numberEvent) {
	$table = $this->getTable();
	$select = $table->select()->order('ts_creation DESC')->limit($numberEvent, 0);

	return $table->fetchAll($select);
    }

    public function getEvent($id_event) {
	$table = $this->getTable();
	$select = $table->select()->where('id_event = ?', $id_event);
	return $table->fetchRow($select);
    }

    public function searchEvent($search) {
	$table = $this->getTable();
	
	$select = $table->select()
			->order('ts_creation DESC')
			->where('nom_event LIKE ?', '%' . $search . '%');

	return $table->fetchAll($select);
    }
}
